package com.creatingrevolutions.vbukit.lib;

import com.creatingrevolutions.vbukit.lib.handler.BaseVbukitHandler;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class CustomSeekbar
{
	boolean DEBUG = false;
	
	int mResBackLine;
	int mResCircle;
	ViewGroup mParent;
	Context mContext;
	
	TextView mBackLine;
	ImageView mButton;
	
	RelativeLayout mLayout;
	
	Bitmap mBitmapButton;
	Bitmap mBitmapBackLine;
	int mBbuttonWidth;
	
	int mTotalVolume;
	int mTotalPlacesForButton;
	int mCurrentPlace = 0;
	
	int mMarginTop = 0;
	int mMarginLeft = 0;
	int mMarginBottom = 0;
	int mMarginRight = 0;
	
	BaseVbukitHandler mHander;
	
	int mTotalParts = 5;
	
	public CustomSeekbar(ViewGroup parent, Context context, int resBackLine,
			int resCircle)
	{
		mParent = parent;
		mResBackLine = resBackLine;
		mResCircle = resCircle;
		mContext = context;
		
		mBackLine = new TextView(mContext);
		mButton = new ImageView(mContext);
		
	}
	
	public void setMargin(int left, int top, int right, int bottom) 
	{
		mMarginLeft = left;
		mMarginTop = top;
		mMarginRight = right;
		mMarginBottom = bottom;
	}

	public void setListener(BaseVbukitHandler handler) {
		mHander = handler;
	}
	
	public void setTotalParts(int parts) {
		mTotalParts = parts;
	}
	
	public void showSlider()
	{
		initBitmaps();
		initLayout();
		initBackLine();
		initButton();
		
		mLayout.addView(mBackLine);
		mLayout.addView(mButton);
	}

	private void initBitmaps()
	{
		// bitmap for button
		Bitmap bitmapButton = BitmapFactory.decodeResource(
				mContext.getResources(), mResCircle);
		
		// bitmap for back line
		Bitmap bitmapBackLine = BitmapFactory.decodeResource(
				mContext.getResources(), mResBackLine);

		// max width for layout and line
		int maxWidth = bitmapBackLine.getWidth() - (mMarginLeft + mMarginRight);
		Log.i("", "Max width: " + maxWidth);

		// bitmap for line
		mBitmapBackLine = Bitmap.createScaledBitmap(bitmapBackLine, maxWidth, 
				bitmapBackLine.getHeight(), false);
		
		// total size of the line
		mTotalVolume = mBitmapBackLine.getWidth();
		
		// button width is 20% of the total length
		int percent = 100 / mTotalParts;
		mBbuttonWidth = mTotalVolume * percent / 100;
		mBbuttonWidth -= percent;
		
		// total places for this button
		mTotalPlacesForButton = (int) mTotalVolume / mBbuttonWidth;
		
		// create bitmao for button
		mBitmapButton = Bitmap.createScaledBitmap(bitmapButton, mBbuttonWidth, 
				mBbuttonWidth, false);
		
		if ( DEBUG )
		{
			Log.i("", "Total volume lenght in pixels: " + mTotalVolume);
			Log.i("", "Volume button has " + mTotalPlacesForButton
					+ " places to move from right to left.");
			Log.i("", "Size for the button: " + mBbuttonWidth);
		}		
	}
	
	private void initLayout()
	{
		mLayout = new RelativeLayout(mContext);
		
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
				mTotalVolume, mBitmapButton.getHeight());
		lp.setMargins(mMarginLeft, mMarginTop, mMarginRight, mMarginBottom);
		
		mLayout.setLayoutParams(lp);
		
		mParent.addView(mLayout);
	}

	
	private void initBackLine()
	{
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
				mTotalVolume, LayoutParams.WRAP_CONTENT);
		
		int top = mBitmapButton.getHeight() / 2 - mBitmapBackLine.getHeight()
				/ 2;
		lp.setMargins(mMarginLeft, top, mMarginRight, 0);
		
		mBackLine.setLayoutParams(lp);
		mBackLine.setBackgroundResource(mResBackLine);
	}
	
	private void initButton()
	{
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		
		mButton.setLayoutParams(lp);
		mButton.setImageResource(mResCircle);
		
		mButton.setOnTouchListener(new OnSwipeTouchListener() {
		    public void onSwipeTop() {
		        if (DEBUG) Log.i("", "top");
		    }
		    public void onSwipeRight() {
		    	if (DEBUG) Log.i("", "right");
		    	moveButton(true);
		    }
		    public void onSwipeLeft() {
		    	if (DEBUG) Log.i("", "left");
		    	moveButton(false);
		    }
		    public void onSwipeBottom() {
		    	if (DEBUG) Log.i("", "bottom");
		    }
		});

		
	}
	
	private void moveButton(boolean goRight)
	{
		boolean canGoRight = mCurrentPlace + 1 < mTotalPlacesForButton;
		boolean canGoLeft = mCurrentPlace - 1 >= 0;
		
		if ( goRight && canGoRight ) {
			mCurrentPlace ++;
			moveButtonToPlace(mCurrentPlace);
		}
		else if ( goRight == false && canGoLeft ) {
			mCurrentPlace --;
			moveButtonToPlace(mCurrentPlace);
		}
	}
	
	private void moveButtonToPlace(int placeNumber)
	{
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		int left = mCurrentPlace * mBitmapButton.getWidth();
		lp.setMargins(left, 0, 0, 0);
		mButton.setLayoutParams(lp);
		
		if ( mHander != null ) {
			mHander.updateInfo(true, null, mCurrentPlace);
		}
	}
	
}
